package com.sdp.right.control;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.SnowFlake;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.db.sys_menu_button.service.ISysMenuButtonService;
import com.sdp.right.entity.SysMenuButton;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-10-16
 */
@RestController
@RequestMapping("/SysMenuButtonController")
public class SysMenuButtonController {

	@Autowired
	private Query query;
	
	@Autowired
	private ISysMenuButtonService iSysMenuButtonService;
	
	@Autowired
	private SnowFlake snowFlake;
	
	@PostMapping(value = "/page")
	public Result<?> queryPageList(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iSysMenuButtonService, SysMenuButton.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody SysMenuButton sysMenuButton) {
		if(sysMenuButton==null) {
			return Result.error("按钮信息为空");
		}
		
		if(!StringUtils.isEmpty(sysMenuButton.getRoleNo())) {
			sysMenuButton.setQueryRoleNo("["+sysMenuButton.getRoleNo().replaceAll(",", "][")+"]");
		}
		
		if(StringUtils.isEmpty(sysMenuButton.getKeyId())) {
			sysMenuButton.setBtNo(snowFlake.createOrderNo());
			iSysMenuButtonService.save(sysMenuButton);
		}else {
			iSysMenuButtonService.updateById(sysMenuButton);
		}
		return Result.ok();
	}
	
	@GetMapping(value = "/del")
	@DS(value="right")
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		iSysMenuButtonService.removeById(keyId);
		return Result.ok();
	}
	
}

